import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import csv
import random
import seaborn

scores = np.array([1, 2, 3, 4, 5])
tss_score_basis = np.array([2, 20, 50, 100, 184])

def mapValues(inArray):
    outArray =  np.interp(inArray, tss_score_basis, scores)
    return outArray

fit_y = np.arange(min(tss_score_basis),max(tss_score_basis),1)
fit_x = mapValues(fit_y)

reader = csv.reader(open(r'C:\Users\jburns\Documents\Python\loading.csv', 'rb'), delimiter=',', quotechar='"')
lst = list(reader)
for line in list(lst):
    if line[0][0] == '#':
        lst.remove(line)

lst = np.array(zip(*lst[1:]))

landuses = np.array(lst[0])
mean = np.array(lst[1], dtype='f')
lci = np.array(lst[2], dtype='f')
uci = np.array(lst[3], dtype='f')

mean_scores = mapValues(mean)
lci_scores = mapValues(lci)
uci_scores = mapValues(uci)

reader = csv.reader(open(r'C:\Users\jburns\Documents\Python\bmp_effectiveness.csv', 'rb'), delimiter=',', quotechar='"')
lst = list(reader)
for line in list(lst):
    if line[0][0] == '#':
        lst.remove(line)

lst = np.array(zip(*lst[1:]))
bmps = np.array(lst[0])
bmp_mean = np.array(lst[1], dtype='f')
bmp_low = np.array(lst[2], dtype='f')
bmp_high = np.array(lst[3], dtype='f')

bmp_mean_scores = mapValues(bmp_mean)
bmp_low_scores = mapValues(bmp_low)
bmp_high_scores = mapValues(bmp_high)

plt.plot(scores, tss_score_basis, 'o', color='royalblue', label="TSS Risk Score Basis")
plt.plot(fit_x, fit_y, ':', color='royalblue')#, label='TSS Risk Score Basis')

luColor = "mediumseagreen"
bmpColor = "burlywood"

plt.errorbar(
    mean_scores, mean,
    yerr=[mean - lci, uci - mean],
    xerr=[mean_scores - lci_scores, uci_scores - mean_scores],
    label='Land Use Concentrations', fmt='o', color=luColor, ecolor=luColor)

plt.errorbar(
    bmp_mean_scores, bmp_mean,
    yerr=[bmp_mean - bmp_low, bmp_high - bmp_mean],
    xerr=[bmp_mean_scores - bmp_low_scores, bmp_high_scores - bmp_mean_scores],
    label='BMP Effluent Concentrations', fmt='^', color=bmpColor, ecolor=bmpColor)

plt.title("TSS Pollutant Loading Overview")

for label, x, y in zip(landuses, mean_scores, mean):
    plt.annotate(label, xy=(x, y), xytext=(20,-20), textcoords='offset points', ha='left', va='top',
        bbox=dict(boxstyle='round,pad=0.2', fc=luColor, alpha=0.3),
        arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',  color=luColor)).draggable()

for label, x, y in zip(bmps, bmp_mean_scores, bmp_mean):
    plt.annotate(label, xy=(x, y), xytext=(-50+random.randrange(-25, 25),50+random.randrange(-25,25)), textcoords='offset points', ha='center', va='bottom',
        bbox=dict(boxstyle='round,pad=0.2', fc=bmpColor, alpha=0.3),
        arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',  color=bmpColor)).draggable()

plt.xlabel('Magnitude of Failure Score')
plt.ylabel('TSS (mg/l)')

plt.axis([0, 5, 0, 200])

plt.legend(loc='upper left')

plt.show()

